﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Xml.Linq;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult GetOperationStatus(String SubscriptionID, String CertificatePath, string RequestID)
        {
            DataAccessResult result = new DataAccessResult();

            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/operations/" + RequestID;
                        
            string ReturnBody = string.Empty;


            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "GET";
                request.ClientCertificates.Add(cert);
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }


                resp = request.GetResponse();
               


                if (resp != null)
                {

                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {
                        List<Entities.HostedService> HostServiceList = new List<Entities.HostedService>();
                        XElement el = XElement.Parse(ReturnBody);
                        Entities.OperationStatus operationStatus = new Entities.OperationStatus();
                        foreach (XElement e1 in el.Elements())
                        {
                            string ElementName = e1.Name.LocalName.Trim().ToLower();
                            string ElementValue = e1.Value;

                            switch (ElementName)
                            {
                                case "status":
                                    operationStatus.Status = ElementValue;
                                    break;
                                case "httpstatuscode":
                                    operationStatus.HTTPCode = ElementValue;
                                    break;
                                case "id":
                                    operationStatus.ID = ElementValue;
                                    break;
                                case "error":
                                    foreach (XElement e2 in e1.Elements())
                                    {
                                        string ElementName1 = e2.Name.LocalName.Trim().ToLower();
                                        string ElementValue1 = e2.Value;
                                        switch (ElementName)
                                        {
                                            case "code":
                                                operationStatus.Code = ElementValue1;
                                                break;
                                            case "message":
                                                operationStatus.Message = ElementValue1;
                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                        result.IsOperationSuccessful = true;
                        result.OperationStatus = operationStatus;
                    }
                }
            }
            catch (WebException webEx)
            {
                result.Error = webEx;
                result.IsOperationSuccessful = false;
            }
            catch (Exception excep)
            {
                result.Error = excep;
                result.IsOperationSuccessful = false;
            }
            finally
            {
                //req = null;
                if (resp != null) { resp.Close(); }
                resp = null;
            }
            return result;
        }
    }
}
